package com.woohua.thread.countdownlatch;

import java.util.concurrent.CountDownLatch;

/***
 * @title MakeRice
 * @description
 * @author woo hua
 * @version 1.0.0
 * @create 2023/3/17 14:16
 **/
public class MakeRice implements Runnable {

    private CountDownLatch countDownLatch;

    public MakeRice(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        exec();

    }

    /**
     * 模拟模板
     */
    private void exec() {

        try {
            System.out.println("Start making rice...");

            long millis = ((int) (1 + Math.random() * (5 - 1 + 1))) * 1000;
            Thread.sleep(millis);
            System.out.println("Making rice is finished");

            // 待处理事情减1
            this.countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
